import request from '../../util/request'
import PubSub from 'pubsub-js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [], //轮播图信息
    recommendList: [], //推荐歌单
    topList: [], //排行榜
    song: {},
    index: 0,
    mold: 0,
    musicRecommend:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    //  发送请求获取轮播图数据
    let bannerListData = await request('/banner', {
      type: 2
    });
    this.setData({
      bannerList: bannerListData.banners
    })
    //获取推荐歌单
    let recommendListData = await request('/personalized', {
      limit: 10
    });
    this.setData({
      recommendList: recommendListData.result
    })
    //获取排行榜
    let index = 0;
    let resultArr = [];
    while (index < 5) {
      let topListData = await request('/top/list', {
        idx: index++
      });
      // splice(会修改原数组，可以对指定的数组进行增删改) slice(不会修改原数组)
      let topListItem = {
        name: topListData.playlist.name,
        index:index-1,
        tracks: topListData.playlist.tracks.slice(0, 5)
      };
      resultArr.push(topListItem);
      // 不需要等待五次请求全部结束才更新，用户体验较好，但是渲染次数会多一些
      this.setData({
        topList: resultArr
      })
    }
    // 消息订阅
    PubSub.subscribe('switchTypeIndex', (_, type) => {
      let {
        topList,
        index,
        mold 
      } = this.data
      if (type === 'pre') {
        (index === 0) && (index = topList[mold].tracks.length)
        index -= 1;
      } else {
        (index === topList[mold].tracks.length - 1) && (index = -1)
        index += 1;
      }
      this.setData({
        index
      })
      let musicId = topList[mold].tracks[index].id
      PubSub.publish('musicIdIndex', musicId)
    })
  },

  //跳转到每日推荐页面
  toRecommendSong() {
    wx.navigateTo({
      url: '/songPackage/pages/recommendSong/recommendSong',
    })
  },
  //跳转到歌曲详情
  toSongDetail(event) {
    let {
      song,
      index,
      mold
    } = event.currentTarget.dataset
    this.setData({
      song,
      index,
      mold
    })
    let title=1
    //路由跳转传参 query参数
    wx.navigateTo({
      // url: '/pages/songDetail/songDetail?song='+JSON.stringify(song),
      url: '/songPackage/pages/songDetail/songDetail?musicId=' + song.id+'&title='+title
    })
  },
  // 跳转到推荐歌曲歌单
  toRecommend(event){
    let {
      item
    } = event.currentTarget.dataset
    this.setData({
      musicRecommend:item
    })
    //路由跳转传参 query参数
    wx.navigateTo({
      url: '/recommendPackage/pages/recommendDetail/recommendDetail?recommendId=' + item.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成 
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})